package edu.lhh;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.lhh.dao.teachtypedao;
import edu.lhh.model.teachtype;

/**
 * Servlet implementation class loginServlet
 */
@WebServlet("/login")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private teachtypedao dao = new teachtypedao();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		response.setCharacterEncoding("utf-8");
		teachtype tp = new teachtype();
		tp = null;
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
		try {
			tp = dao.findbyname(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String epwd ="密码错误";// String("密码错误".getBytes("ISO-8859-1"),"UTF-8");
		String ecode ="验证码错误";//= new String("验证码错误".getBytes("ISO-8859-1"),"UTF-8");
		
		String cod = request.getParameter("code");
		Object obj = session.getAttribute("code");		
		out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
		
		if(obj!=null)
			if(cod.equals(obj.toString())){
				if(id.equals(tp.getUser()) && pwd.equals(tp.getPwd())){
					request.getRequestDispatcher("main.jsp").forward(request, response);
					session.setAttribute("tname", tp);
				}
				else{
					out.println(epwd);

				}
	
			}
			else{
				out.println(ecode);
			}
		//System.out.println(tp.getPwd());
		//System.out.println(pwd);
		//System.out.println(tp.getUser());
		//System.out.println(id);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

}
